package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.OrderDetailMapper;
import com.spark.incoicings.dao.PurchaseDetailsMapper;
import com.spark.incoicings.service.PurchaseDetailService;
import com.spark.invoicings.entity.PurchaseDetail;
import com.spark.invoicings.entity.SalesOrder;
import com.spark.invoicings.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseDetailServiceImpl
        extends ServiceImpl<PurchaseDetailsMapper, PurchaseDetail>
        implements PurchaseDetailService {

    @Resource
    private PurchaseDetailsMapper purchaseDetailMapper;

    @Resource
    private OrderDetailMapper orderDetailMapper;



    @Override
    public List<PurchaseDetail> selectPurchaseDetailStatusAll() {
        return purchaseDetailMapper.selectPurchaseDetailStatusAll();
    }

    @Override
    public List<PurchaseDetail> selectDetailById(Integer id) {
        return purchaseDetailMapper.selectDetailById(id);
    }

    /**
     * 根据id查询采购明细
     * @param id
     * @return
     */
    @Override
    public PurchaseDetail selectPurchaseDetailsById(Integer id) {
        return purchaseDetailMapper.selectPurchaseDetailsById(id);
    }

    /**
     * 查询所有采购明细
     * 分页
     * 根据订单号模糊查询
     * @param map
     * @return
     */
    @Override
    public List<PurchaseDetail> selectPurchaseDetailAll(Map<String, Object> map) {
        // selectOrderDetailsSalesById
        List<PurchaseDetail> list = purchaseDetailMapper.selectPurchaseDetailAll(map);
        for (PurchaseDetail purchaseDetail : list) {
            SalesOrder salesOrder = orderDetailMapper.selectOrderDetailsSalesById(purchaseDetail.getOrderDetail().getSalesOrderId());
            purchaseDetail.setSalesOrder(salesOrder);
        }
        return list;
    }

    /**
     * 添加采购明细
     * @param purchaseDetail
     * @return
     */
    @Override
    public Result addPurchaseDetail(PurchaseDetail purchaseDetail) {
        // 计算总金额
        double count =  purchaseDetail.getPurchaseQuantity() * purchaseDetail.getPurchasePrice();
        purchaseDetail.setTotalPurchaseAmount(count);
        // 待到货数量
        purchaseDetail.setPendingArrivalQuantity(purchaseDetail.getPurchaseQuantity());
        // 应付金额
        purchaseDetail.setPayableAmount(count);
        // 待付款金额
        purchaseDetail.setPendingPaymentAmount(count);
        purchaseDetailMapper.insert(purchaseDetail);
        return Result.success(200, "success", purchaseDetail.getPurchaseDetailId());
    }

    /**
     * 根据id更新采购明细
     * @param purchaseDetail
     * @return
     */
    @Override
    public boolean updatePurchaseDetailById(PurchaseDetail purchaseDetail) {
        // 计算总金额
        double count =  purchaseDetail.getPurchaseQuantity() * purchaseDetail.getPurchasePrice();
        purchaseDetail.setTotalPurchaseAmount(count);
        // 待到货数量
        purchaseDetail.setPendingArrivalQuantity(purchaseDetail.getPurchaseQuantity());
        // 应付金额
        purchaseDetail.setPayableAmount(count);
        // 待付款金额
        purchaseDetail.setPendingPaymentAmount(count);
        return purchaseDetailMapper.updateById(purchaseDetail) > 0;
    }
}
